<!-- pages/find/find.vue -->
<template>
  <div class="container">
    <!-- 简历列表 -->
    <div class="section">
      <h3 class="section-title">点击简历项，跳转到开始面试</h3>

      <!-- 上传组件 -->
      <div class="upload-container">
        <button class="upload-btn" @click="handleUpload">上传简历</button>
      </div>

      <div v-if="resumeList.length" class="list">
        <div
          v-for="(item, index) in resumeList"
          :key="item.resume_id"
          class="list-item"
          @click="navigateToInterview(item.fileids,item.resume_id)"
        >
          {{ `${index + 1}. ${parseSummary(item.summary)}` }}
        </div>
      </div>
      <div v-else class="loading">无简历</div>
    </div>

    <!-- 面试列表 -->
    <div class="section">
      <h3 class="section-title">已完成的面试列表，点击跳转详情</h3>
      <div v-if="interviewList.length" class="list">
        <div
          v-for="item in interviewList"
          :key="item.interview_id"
          class="list-item"
          @click="navigateToSummary(item.interview_id)"
        >
          <div class="title">{{ parseSummary(item.summary) }}</div>
          <div class="note">
            职位: {{ item.position }} | 结束时间: {{ item.end_time }}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { findResume, findInterview, uploadFile } from "@/utils/find";
import { onLoad } from "@dcloudio/uni-app";

const resumeList = ref([]);
const interviewList = ref([]);
const isUploading = ref(false);
const uploadProgress = ref(0);
const tempFilePath = ref("");

const handleUpload = async () => {
    // 选择文件
    const res = await uni.chooseFile({
      count: 1,
      type: "all",
    });
    
    // 检查是否选择了文件
    if (!res.tempFiles || res.tempFiles.length === 0) {
      uni.showToast({
        title: '未选择文件',
        icon: 'none',
        duration: 2000
      });
      return;
    }
    
    const file = res.tempFiles[0];
    console.log("选择的文件路径：", file.path);

    // 开始上传
    isUploading.value = true;
    uploadProgress.value = 0;

    // 调用上传接口
    const result = uploadFile(file.path);

    // 更新上传进度
    if (result.progress) {
      uploadProgress.value = result.progress;
    }

    // 上传成功
    uni.showToast({ title: "上传成功，总结简历需要稍后更新" });


    // 刷新简历列表
    const updatedResumes = await findResume();
    resumeList.value = updatedResumes;
    isUploading.value = false;
    console.log('刷新')
    // 延迟 0.5 秒后刷新页面
    setTimeout(() => {
      uni.reLaunch({
        url: '/pages/index/find'  // 当前页面路径
      });
    }, 200);
};

// 解析嵌套的JSON字符串
const parseSummary = (str) => {
    // 特殊处理 "null" 字符串
  if (str === "null") {
    return "简历正在总结中";
  }
  

  try {
    const parsed = JSON.parse(str);
    return parsed.summary || "无摘要信息";
  } catch {
    return str || "无摘要信息";
  }
};

// 跳转到面试页面
const navigateToInterview = (fileids,resume_id) => {
  uni.navigateTo({
    url: `/pages/interview/interview?fileids=${fileids}&resume_id=${resume_id}`,
  });
};

// 跳转到详情页面
const navigateToSummary = (interviewId) => {
  uni.navigateTo({
    url: `/pages/summary/summary?interview_id=${interviewId}`,
  });
};

// 初始化加载数据
onLoad(async () => {
  try {
    const [resumes, interviews] = await Promise.all([findResume(), findInterview()]);
    resumeList.value = resumes;
    interviewList.value = interviews;
  } catch (error) {
    uni.showToast({ title: "数据加载失败", icon: "none" });
  }
});
</script>

<style scoped>
.container {
  min-height: 100vh;
  padding: 20px;
}

.section {
  margin-bottom: 30px;
  border: 1px solid #eee;
  border-radius: 8px;
  padding: 15px;
}

.section-title {
  color: #333;
  font-size: 16px;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid #eee;
}

.list {
  border-radius: 6px;
  overflow: hidden;
}

.list-item {
  padding: 12px;
  border-bottom: 1px solid #f5f5f5;
  cursor: pointer;
  transition: background-color 0.2s;
}

.list-item:hover {
  background-color: #f9f9f9;
}

.title {
  font-weight: 500;
  color: #333;
}

.note {
  font-size: 12px;
  color: #666;
  margin-top: 4px;
}

.loading {
  text-align: center;
  color: #999;
  padding: 20px;
}

.upload-container {
  margin-bottom: 15px;
  padding: 10px;
  background: #f8f8f8;
  border-radius: 6px;
}

.upload-btn {
  width: 100%;
  padding: 12px;
  background: #4caf50;
  color: white;
  border-radius: 4px;
  font-size: 14px;
}

.upload-status {
  margin-top: 8px;
  text-align: center;
  color: #333;
}
</style>
